package com.ruoyi.web.cp.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.web.constant.Constants;
import com.ruoyi.web.core.config.ConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.web.constant.Constants.*;

@Slf4j
@Service
public class SendFitService {
    @Autowired
    private ConfigLoader configLoader;
    @Autowired
    private WinxinService winxinService;

    public void sendRmobFbFit() {
        String[][] fileConfigs150Ob = {
                {configLoader.getRmPath() + "rmob-merge150.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmob-merge-half150.txt", "半场波胆"}
        };
        processFiles(fileConfigs150Ob, "rm-ob", RMOB_150_MD5_3, "rmobFit,150+发送的值:{}",
                "707e11f4-1d60-45ae-bcd7-1f6028210b0c", Constants::setRMOB_150_MD5_3, false);

        String[][] fileConfigs150Fb = {
                {configLoader.getRmPath() + "rmfb-merge150.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmfb-merge-half150.txt", "半场波胆"}
        };
        processFiles(fileConfigs150Fb, "rm-fb", RMFB_150_MD5_3, "rmfbFit,150+发送的值:{}",
                "707e11f4-1d60-45ae-bcd7-1f6028210b0c", Constants::setRMFB_150_MD5_3, false);
    }

    public void sendRmobFit() {
        // 定义不同的文件路径和标签组合
        String[][] fileConfigs30 = {
                {configLoader.getRmPath() + "rmob-merge30.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmob-merge-half30.txt", "半场波胆"},
                {configLoader.getRmPath() + "rmob-merge-accurate30.txt", "全场波胆特殊"}
        };
        processFiles(fileConfigs30, "rm-ob", RMOB_30_MD5, "rmobFit,30+发送的值:{}", "1000006", Constants::setRMOB_30_MD5);
        processFiles(fileConfigs30, "rm-ob", RMOB_30_MD5_2, "rmobFit,30+发送的值:{}", "47795995-64e7-40ac-ac2e-46108cfe0f21", Constants::setRMOB_30_MD5_2);

        String[][] fileConfigs150 = {
                {configLoader.getRmPath() + "rmob-merge150.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmob-merge-half150.txt", "半场波胆"}
        };
        processFiles(fileConfigs150, "rm-ob", RMOB_150_MD5, "rmobFit,150+发送的值:{}", "1000014", Constants::setRMOB_150_MD5);
        processFiles(fileConfigs150, "rm-ob", RMOB_150_MD5_2, "rmobFit,150+发送的值:{}", "c328724f-232d-486b-a88e-45a520922143", Constants::setRMOB_150_MD5_2);

        String[][] fileConfigs100 = {
                {configLoader.getRmPath() + "rmob-merge100.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmob-merge-half100.txt", "半场波胆"}
        };
        processFiles(fileConfigs100, "rm-ob", RMOB_100_MD5, "rmobFit,100+发送的值:{}", "1000017", Constants::setRMOB_100_MD5);
        processFiles(fileConfigs100, "rm-ob", RMOB_100_MD5_2, "rmobFit,100+发送的值:{}", "8765615d-931c-4264-9b8d-27744e7104d1", Constants::setRMOB_100_MD5_2);

        String[][] fileConfigsDouble = {
                {configLoader.getRmPath() + "rmob-merge-double1.5.txt", "正波双进<1.7"}
        };
        processFiles(fileConfigsDouble, "rm-ob", RMOB_DOUBLE_MD5, "rmobDouble,<1.7发送的值:{}", "1000016", Constants::setRMOB_DOUBLE_MD5);

        String[][] fileConfigsDy = {
                {configLoader.getRmPath() + "rmob-merge-dy7.txt", "正波独平>4"}
        };
        processFiles(fileConfigsDy, "rm-ob", RMOB_DY_MD5, "rmobDy,>4发送的值:{}", "1000015", Constants::setRMOB_DY_MD5);

        String[][] fileConfigsDx2 = {
                {configLoader.getRmPath() + "rmob-merge-dx2-5.txt", "正波首5小球"}
        };
        processFiles(fileConfigsDx2, "rm-ob", RMOB_DX2_MD5, "rmobDx2,<5发送的值:{}", "1000003", Constants::setRMOB_DX2_MD5);
    }

    public void sendAqobFit() {
        // 定义不同的文件路径和标签组合
        String[][] fileConfigs30 = {
                {configLoader.getRmPath() + "aqob-merge30.txt", "全场波胆"},
                {configLoader.getRmPath() + "aqob-merge-accurate30.txt", "全场波胆特殊"}
        };
        processFiles(fileConfigs30, "aq-ob", AQOB_30_MD5, "aqobFit,30+发送的值:{}", "0fa17f8d-3b74-46cd-8540-25aed734fd37", Constants::setAQOB_30_MD5);

        String[][] fileConfigs150 = {
                {configLoader.getRmPath() + "aqob-merge150.txt", "全场波胆"},
        };
        processFiles(fileConfigs150, "aq-ob", AQOB_150_MD5, "aqobFit,150+发送的值:{}", "240efa37-42d0-4aab-8ff6-b6e2e2b0eb30", Constants::setAQOB_150_MD5);

        String[][] fileConfigs100 = {
                {configLoader.getRmPath() + "aqob-merge100.txt", "全场波胆"},
        };
        processFiles(fileConfigs100, "aq-ob", AQOB_100_MD5, "aqobFit,100+发送的值:{}", "5fa8b2c2-c89d-48c6-be55-4ee778296c23", Constants::setAQOB_100_MD5);
    }

    public void sendAqfbFit() {
        // 定义不同的文件路径和标签组合
        String[][] fileConfigs30 = {
                {configLoader.getRmPath() + "aqfb-merge30.txt", "全场波胆"},
        };
        processFiles(fileConfigs30, "aq-fb", AQFB_30_MD5, "aqfbFit,30+发送的值:{}", "0fa17f8d-3b74-46cd-8540-25aed734fd37", Constants::setAQFB_30_MD5);

        String[][] fileConfigs150 = {
                {configLoader.getRmPath() + "aqfb-merge150.txt", "全场波胆"},
        };
        processFiles(fileConfigs150, "aq-fb", AQFB_150_MD5, "aqfbFit,150+发送的值:{}", "240efa37-42d0-4aab-8ff6-b6e2e2b0eb30", Constants::setAQFB_150_MD5);

        String[][] fileConfigs100 = {
                {configLoader.getRmPath() + "aqfb-merge100.txt", "全场波胆"},
        };
        processFiles(fileConfigs100, "aq-fb", AQFB_100_MD5, "aqfbFit,100+发送的值:{}", "5fa8b2c2-c89d-48c6-be55-4ee778296c23", Constants::setAQFB_100_MD5);
    }

    private void processFiles(String[][] fileConfigs, String prefix, String md5, String logMessage,
                              String messageId, MD5Setter md5Setter) {
        processFiles(fileConfigs, prefix, md5, logMessage, messageId, md5Setter, true);
    }

    private void processFiles(String[][] fileConfigs, String prefix, String md5, String logMessage,
                              String messageId, MD5Setter md5Setter, boolean isAll) {
        List<String> list = new ArrayList<>();
        try {
            for (String[] config : fileConfigs) {
                readFitFilt(config[0], list, config[1], isAll);
            }
            if (CollUtil.isNotEmpty(list)) {
                list.add(0, prefix);
                String str = CollUtil.join(list, "\n");
                String hash = Md5Utils.hash(str);

                // 检查 MD5 是否变化
                if (!md5.equals(hash)) {
                    md5Setter.set(hash);
                    log.info(logMessage, str);

                    int maxStrLength = 1300;
                    // 检查字符串长度是否超过1300字符
                    if (str.length() > maxStrLength) {
                        // 保留第一个元素
                        String firstElement = list.remove(0);
                        // 分割list
                        List<List<String>> subLists = splitList(list, maxStrLength);

                        // 发送每个子列表
                        for (int i = 0; i < subLists.size(); i++) {
                            List<String> subList = subLists.get(i);
                            subList.add(0, firstElement + StrUtil.format("({}/{})", i + 1, subLists.size()));
                            String subStr = CollUtil.join(subList, "\n");
                            if (messageId.length() > 8) {
                                winxinService.sendQlMsg(messageId, subStr);
                            } else {
                                winxinService.sendTextMessage(messageId, subStr);
                            }
                        }
                    } else {
                        if (messageId.length() > 8) {
                            winxinService.sendQlMsg(messageId, str);
                        } else {
                            winxinService.sendTextMessage(messageId, str);
                        }
                    }
                } else {
                    log.info("文件路径：{}, MD5值相同，不发送", fileConfigs[0][0]);
                }
            }
        } catch (Exception e) {
            log.error("处理文件时发生异常", e);
        }
    }

    private List<List<String>> splitList(List<String> list, int maxLength) {
        List<List<String>> subLists = new ArrayList<>();
        StringBuilder currentSubList = new StringBuilder();

        for (String item : list) {
            if (currentSubList.length() + item.length() + 1 > maxLength) {
                subLists.add(CollUtil.newArrayList(currentSubList.toString().split("\n")));
                currentSubList.setLength(0);
            }
            currentSubList.append(item).append("\n");
        }

        if (currentSubList.length() > 0) {
            subLists.add(CollUtil.newArrayList(currentSubList.toString().split("\n")));
        }

        return subLists;
    }

    private void readFitFilt(String path, List<String> list, String flag, boolean isAll) {
        List<String> strs = FileUtil.readLines(path, CharsetUtil.charset("utf8"));
        if (CollUtil.isNotEmpty(strs)) {
            list.add(flag);
            for (String str : strs) {
                if (str.startsWith("2025")) {
                    String[] parts = str.split(",");
                    //不发送全部，只发送部分
                    if (!isAll) {
                        String format = DateUtil.format(DateUtil.offsetDay(new Date(), 1), "yyyy-MM-dd");
                        if (parts[0].startsWith(format)) {
                            return;
                        }
                    }
                    if (parts.length == 5) {
                        // 删除最后一个元素
                        String[] trimmedParts = new String[4];
                        System.arraycopy(parts, 0, trimmedParts, 0, 4);
                        str = String.join(",", trimmedParts);
                    }
                }
                list.add(str);
            }
            list.add(""); // 添加空行分隔
        }
    }

    public void sendRmfbFit() {
        String[][] fileConfigs30 = {
                {configLoader.getRmPath() + "rmfb-merge30.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmfb-merge-half30.txt", "半场波胆"}
        };
        processFiles(fileConfigs30, "rm-fb", RMFB_30_MD5, "rmfbFit,30+发送的值:{}", "1000006", Constants::setRMFB_30_MD5);
        processFiles(fileConfigs30, "rm-fb", RMFB_30_MD5_2, "rmfbFit,30+发送的值:{}", "47795995-64e7-40ac-ac2e-46108cfe0f21", Constants::setRMFB_30_MD5_2);

        String[][] fileConfigs150 = {
                {configLoader.getRmPath() + "rmfb-merge150.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmfb-merge-half150.txt", "半场波胆"}
        };
        processFiles(fileConfigs150, "rm-fb", RMFB_150_MD5, "rmfbFit,150+发送的值:{}", "1000014", Constants::setRMFB_150_MD5);
        processFiles(fileConfigs150, "rm-fb", RMFB_150_MD5_2, "rmfbFit,150+发送的值:{}", "c328724f-232d-486b-a88e-45a520922143", Constants::setRMFB_150_MD5_2);

        String[][] fileConfigs100 = {
                {configLoader.getRmPath() + "rmfb-merge100.txt", "全场波胆"},
                {configLoader.getRmPath() + "rmfb-merge-half100.txt", "半场波胆"}
        };
        processFiles(fileConfigs100, "rm-fb", RMFB_100_MD5, "rmfbFit,100+发送的值:{}", "1000017", Constants::setRMFB_100_MD5);
        processFiles(fileConfigs100, "rm-fb", RMFB_100_MD5_2, "rmfbFit,100+发送的值:{}", "8765615d-931c-4264-9b8d-27744e7104d1", Constants::setRMFB_100_MD5_2);

    }

    @FunctionalInterface
    interface MD5Setter {
        void set(String hash);
    }
}
